/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.tools.CachedLoadedImage;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;

	/**
	 * Load and hold image.
	 * 
	 * @author Marek Brun
	 */
	public class CachedLoadedImageView extends MLBroadcastClass implements IMLBroadcasterListener {
		
		public const event_PasteImage : String = 'PasteImage';
		
		public var holder:Bitmap;
		private var model:CachedLoadedImage;

		/**
		 * View of {@link CachedLoadedImage}
		 */
		public function CachedLoadedImageView(holder:Bitmap) {
			this.holder=holder;
			
		}
		
		public function getProgress():Number {
			return model.getProgress();
		}
		
		public function getHolder():Bitmap {
			return holder;
		}
		
		/**
		 * Model can be set multiple times.
		 */
		public function setModel(model:CachedLoadedImage, isStartLoad:Boolean=true):void {
			if(this.model==model){ return; }
			removeModel();
			this.model=model;
			if(model.isSuccess){
				pasteImage();
			}else{
				model.addListener(this);
				if(isStartLoad){
					model.start();
				}
			}
		}
		
		/**
		 * @return current model
		 */
		public function getModel():CachedLoadedImage {
			return model;
		}
		
		public function removeModel():void {
			if(model){
				model.removeListener(this);
				holder.bitmapData=null;
				model=null;
			}
		}
		
		protected function pasteImage():void {
			holder.bitmapData=model.getBitmapData();
			holder.smoothing=true;
			broadcastEvent(event_PasteImage);
		}
		
		public static function createInstanceWithHolderInMC(display:DisplayObjectContainer):CachedLoadedImageView {
			var holder:Bitmap=new Bitmap(null, "auto", true);
			display.addChild(holder);
			return new CachedLoadedImageView(holder);
		}
		
//********************************************************************************************
//		events for CachedLoadedImageView
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case model:
					switch(ev.event) {
						case model.event_ActionFinish:
							if(model.isSuccess){
								pasteImage();
							}else{
								dbg.log('Fail to load image from url:'+model.getURL());
							}
						break;
					}
				break;
			}
		}
		
	}

}
